#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>
#include<QImage>
#include<QFileDialog>
#include "uimgr.h"
#include "easylogging++.h"
using namespace cv;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);

    testThread = new TestThread(UIMgr::ThreadBack);  //分配空间
    connect(testThread, SIGNAL(newValue(QString)), this, SLOT(lblShow(QString)));
    testThread->start();
    testThread1 = new TestThread(UIMgr::ThreadWork);

    formWelcom =new FormWelcom(this);
    formVideo = new FormVideo(this);

    ui->stackedWidget->addWidget(formWelcom);
    ui->stackedWidget->addWidget(formVideo);
    ui->stackedWidget->setCurrentWidget(formWelcom);

    connect(formWelcom, SIGNAL(switchF()), this, SLOT(switchForm()));
    connect(formVideo, SIGNAL(switchF()), this, SLOT(switchForm()));
    LOG(INFO) << "MainWindow Init";
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::lblShow(QString num){
    ui->labelTime->setText(num);

}

void MainWindow::opencvTest(){
    QTime qTime;
    qTime.start();

    cv::Mat src = cv::imread("D://circle.bmp");
    Mat roi;
    //cvtColor(src, gray, CV_RGB2GRAY);
    roi = src(Rect(400, 800, 300, 270));
    //imshow("gray", gray);
    //初始化参数设置
    SimpleBlobDetector::Params params;
    params.minDistBetweenBlobs = 0.0f;
    params.filterByInertia = false;
    params.filterByColor = false;
    // 声明根据面积过滤，设置最大与最小面积
    params.filterByArea = true;
    params.minArea = 1.0f;
    params.maxArea = 20000.0f;
    // 声明根据圆度过滤，设置最大与最小圆度
    params.filterByCircularity = true;
    params.minCircularity = 0.1;
    params.maxCircularity = 1.0;
    // 凸包形状分析 - 过滤凹包
    params.filterByConvexity = true;
    params.minConvexity = 0.1;
    params.minConvexity = 1.0;

    Mat thre;
    //400 800 270 270
    //double threValue=ui->lineEditThre->text().toDouble();
    //int size=ui->lineEditThre->text().toInt();
    int size=15;
    threshold(roi,thre,30,255,  CV_THRESH_BINARY_INV);
    cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(size, size));
    Mat dst;
    cv::dilate(thre, dst, element);
    //创建BLOB Detector
    Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);

    //Blob 分析与显示
    Mat result;
    std::vector<KeyPoint> keypoints;
    detector->detect(dst, keypoints);
    qDebug()<<keypoints.size();
    for (int var = 0; var < keypoints.size(); ++var) {
        qDebug()<< keypoints[var].size<<keypoints[var].response;
    }
    drawKeypoints(dst, keypoints, result, Scalar(255, 0, 0), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);

    qDebug()<<qTime.elapsed()<<"ms";
}
void MainWindow::switchForm()
{
    int i=ui->stackedWidget->currentIndex();
    //qDebug()<<i<<"|"<<ui->stackedWidget->children().count();
    if(i==0){
        ui->stackedWidget->setCurrentIndex(1);
    }else{
        ui->stackedWidget->setCurrentIndex(0);
    }


}

#pragma region 窗体移动{

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    pressed = true;
    globalPos = event->globalPos();
    basicSize = size();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(pressed){
        QPoint p = event->globalPos();
        if(canMove){
            QSize tempSize = basicSize;
            if(moveType & 1){
                int width = basicSize.width() + p.x() - globalPos.x();
                tempSize.setWidth(width >= 50 ? width : 50);
            }
            if(moveType & 2){
                int height = basicSize.height() + p.y() - globalPos.y();
                tempSize.setHeight(height >= 50 ? height : 50);
            }
            resize(tempSize);
        }else{
            this->move(x() - globalPos.x() + p.x(), y() - globalPos.y() + p.y());
            globalPos = p;
        }
    }else{
        moveType = 3;
        moveType &= (event->pos().x() + 10 >= width() ? 255 : 254);
        moveType &= (event->pos().y() + 10 >= height() ? 255 : 253);
        switch (moveType) {
        case 0: setCursor(Qt::ArrowCursor);break;
        case 1: setCursor(Qt::SizeHorCursor);break;
        case 2: setCursor(Qt::SizeVerCursor);break;
        case 3: setCursor(Qt::SizeFDiagCursor);break;
        default: break;
        }
        canMove = (moveType > 0);
    }
}
void MainWindow::mouseReleaseEvent(QMouseEvent*)
{
    pressed = false;
}

#pragma endregion}
















